//
//  CropImgViewController.m
//  LazyCat
//
//  Created by Abner on 16/5/5.
//  Copyright © 2016年 zhanshu. All rights reserved.
//

#import "CropImgViewController.h"
#import "CPJImageClipping.h"
#import "CPJClippingPanel.h"

@interface CropImgViewController ()

@property (nonatomic, strong)CPJImageClipping *imageClipping;

@end

@implementation CropImgViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initNav:@"裁剪图片" doBack:YES];
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 63, ScreenWidth, 1)];
    lineView.backgroundColor = [UIColor lightGrayColor];
    lineView.alpha = 0.2;
    [self.navBar addSubview:lineView];
    self.view.backgroundColor = BackColor;
    
    //右边的"保存"按钮
    UIButton *completeBt = [UIButton buttonWithType:UIButtonTypeCustom];
    [completeBt setTitle:@"保存" forState:UIControlStateNormal];
    completeBt.frame = CGRectMake(ScreenWidth-8-40, 20, 40, 44);
    [completeBt setHitTestEdgeInsets:UIEdgeInsetsMake(0, -10, 0, -10)];
    [completeBt setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    completeBt.titleLabel.font = FontSet(15);
    [self.navBar addSubview:completeBt];
    [completeBt addTarget:self action:@selector(completeBtClicked) forControlEvents:UIControlEventTouchUpInside];
    
    self.imageClipping = [[CPJImageClipping alloc] init];
    [self addChildViewController:self.imageClipping];
    self.imageClipping.image = self.scropedImg;
    self.imageClipping.view.frame = CGRectMake(0, 64, ScreenWidth, ScreenHeight-64);
    self.imageClipping.view.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.imageClipping.view];
    
    
    self.imageClipping.clippingPanel.backgroundColor = [UIColor whiteColor];
    CGFloat clippingRectY = (ScreenHeight-(ScreenWidth-40)-64)/2;
    self.imageClipping.clippingRect = CGRectMake(20, clippingRectY, ScreenWidth-40, ScreenWidth-40);
    UIImageView *coverView = [[UIImageView alloc] initWithFrame:self.imageClipping.clippingRect];
    [self.imageClipping.view addSubview:coverView];
    [self.imageClipping.clippingPanel setNeedsDisplay];
    
    
    
}
#pragma mark --- "保存"点击事件
-(void)completeBtClicked
{
    UIImage *aImg = [self.imageClipping clippImage];
    if (self.overCropedImgBlock) {
        self.overCropedImgBlock(aImg);

    }
    [self.navigationController popViewControllerAnimated:YES];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
